<?php
/**
 * RcExcel class file.
 *
 * @author         RcPHP Dev Team
 * @copyright      Copyright (c) 2013,RcPHP Dev Team
 * @license        Apache License 2.0 {@link http://www.apache.org/licenses/LICENSE-2.0}
 * @package        libraries
 * @since          1.0
 * @filesource
 */
defined('IN_RCPHP') or exit('Access denied');

class RcExcel extends RcController
{

	/**
	 * excel table xml code
	 *
	 * @var string
	 */
	protected $xmlTable;

	/**
	 * excel title xml code
	 *
	 * @var string
	 */
	protected $xmlMenu;

	/**
	 * __construct method
	 *
	 * @return void
	 */
	public function __construct()
	{
	}

	/**
	 * A line of code in excel.
	 *
	 * @param  array $data
	 * @return string
	 */
	protected function handleRow($data)
	{
		if(empty($data) || !is_array($data))
		{
			return false;
		}

		$xml = "<Row>\n";
		foreach($data as $key => $value)
		{
			$xml .= ($key > 0 && empty($data[$key - 1])) ? $this->handleIndexCell($value, $key + 1) : $this->handleCell($value);
		}
		$xml .= "</Row>\n";

		return $xml;
	}

	/**
	 * Processing EXCEL data code.
	 *
	 * @param array $data
	 * @return string
	 */
	protected function addRows($data)
	{
		if(empty($data) || !is_array($data) || !is_array($data[0]))
		{
			return false;
		}

		$xmlArray = array();
		foreach($data as $row)
		{
			$xmlArray[] = $this->handleRow($row);
		}

		return implode('', $xmlArray);
	}

	/**
	 * Configure the EXCEL table title.
	 *
	 * @param array $data
	 * @return bool
	 */
	public function setMenu($data)
	{
		if(empty($data) || !is_array($data) || is_array($data[0]) || array_search('', $data))
		{
			return false;
		}

		$xml = "<Row>\n";
		foreach($data as $value)
		{
			$type = (is_numeric($data) && (substr($data, 0, 1) != 0)) ? 'Number' : 'String';
			$xml .= "<Cell><Data ss:Type=\"" . $type . "\">" . $value . "</Data></Cell>\n";
		}
		$xml .= "</Row>\n";
		$this->xmlMenu = $xml;

		return true;
	}

	/**
	 * EXCEL table of contents.
	 *
	 * @param array $data
	 * @return string
	 */
	public function setData($data)
	{

		$xmlRows = $this->addRows($data);

		if(empty($xmlRows))
		{
			if(empty($this->xmlMenu))
			{
				return false;
			}
			else
			{
				$content = $this->xmlMenu;
			}
		}
		else
		{
			if(empty($this->xmlMenu))
			{
				$content = $xmlRows;
			}
			else
			{
				$content = $this->xmlMenu . $xmlRows;
			}
		}

		return $this->xmlTable = "<Table>\n" . $content . "</Table>\n";
	}

	/**
	 * The EXCEL table information processing code.
	 *
	 * @return string
	 */
	protected function parseTable()
	{

		$xmlWorksheet = "<Worksheet ss:Name=\"Sheet1\">\n";

		if(empty($this->xmlTable))
		{
			$xmlWorksheet .= "<Table/>\n";
		}
		else
		{
			$xmlWorksheet .= $this->xmlTable;
		}

		$xmlWorksheet .= "</Worksheet>\n";

		return $xmlWorksheet;
	}

	/**
	 * Processing in EXCEL form.
	 *
	 * @param string $data
	 * @return string
	 */
	protected function handleCell($data)
	{
		if(is_array($data))
		{
			return false;
		}

		$type = (is_numeric($data) && (substr($data, 0, 1) != 0)) ? 'Number' : 'String';

		return "<Cell><Data ss:Type=\"" . $type . "\">" . $data . "</Data></Cell>\n";
	}

	/**
	 * Treatment of CELL code in EXCEL, when a CELL content of the pre CELL is empty.
	 *
	 * @param string  $data
	 * @param integer $key
	 * @return string
	 */
	protected function handleIndexCell($data, $key)
	{
		if(is_array($data))
		{
			return false;
		}

		$type = (is_numeric($data) && (substr($data, 0, 1) != 0)) ? 'Number' : 'String';

		return "<Cell ss:Index=\"" . $key . "\"><Data ss:Type=\"" . $type . "\">" . $data . "</Data></Cell>\n";
	}

	/**
	 * Excel header
	 *
	 * @return string
	 */
	protected function parseHeader()
	{

		return "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
 xmlns:o=\"urn:schemas-microsoft-com:office:office\"
 xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
 xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
 xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n";
	}

	/**
	 * Download excel file.
	 *
	 * @param string $fileName
	 * @return void
	 */
	public function download($fileName = null)
	{
		if(is_null($fileName))
		{
			//default file name.
			$fileName = 'export';
		}

		header('Pragma: no-cache');
		header("Content-Type: application/vnd.ms-excel; charset=utf-8");
		header("Content-Disposition: inline; filename=\"" . $fileName . ".xls\"");
		$excelXml = $this->parseHeader() . $this->parseTable() . "</Workbook>";

		echo $excelXml;
	}

	/**
	 * Exit
	 *
	 * @return void
	 */
	public function __destruct()
	{
		exit();
	}
}